﻿using SistemaAcademicoTCCDAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SistemaAcademico.Pages
{
    public partial class NovaAula : System.Web.UI.Page
    {

        SATCCEntities container = new SATCCEntities();


        #region PAGE EVENTS

        void Page_PreRenderComplete(object sender, EventArgs e)
        {
            if (!IsPostBack)
                ddlCursoNaoAtribuido.Items.Insert(0, "Selecione a matéria...");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                if (ddlCursoNaoAtribuido.SelectedIndex > 0)
                {
                    GerarDDLs();
                }
            }
        }

        protected void btnContinuar_Click(object sender, EventArgs e)
        {
            if (Valida())
                foreach (Control ctrl in pnlItensDinamicos.Controls)
                    if (ctrl is DropDownList)
                    {
                        AulaSet aula = new AulaSet();

                        //Add materia a aula
                        String[] splitMateria = ctrl.ID.Split('_');
                        int materiaId = Convert.ToInt32(splitMateria[1]);
                        var materia = container.MateriaSet.Where(m => m.Id == materiaId).First();
                        aula.MateriaSet.Add(materia);

                        //Add professor a aula
                        int professorId = Convert.ToInt32(((DropDownList)ctrl).SelectedValue);
                        var prof = container.ProfessorSet.Where(p => p.Id == professorId).First();
                        aula.Professor_id = professorId;
                        aula.ProfessorSet.Add(prof);

                        //Add turma a aula
                        int turmaId = Convert.ToInt32(ddlCursoNaoAtribuido.SelectedValue);
                        var turma = container.TurmaSet.Where(t => t.Id == turmaId).First();
                        aula.Turma_Id = turmaId;

                        container.AulaSet.AddObject(aula);
                        turma.AulaSet.Add(aula);
                        turma.Atribuido = true;
                        container.SaveChanges();
                        Response.Redirect("~/Pages/Turmas.aspx");
                    }
        }
        #endregion

        #region Methods
        bool Valida()
        {
            if (ddlCursoNaoAtribuido.SelectedIndex == 0)
            {
                Response.Write("<script>alert('Por favor, selecione uma turma.');</script>"); ;
                return false;
            }
            foreach (Control ctrl in pnlItensDinamicos.Controls)
            {
                if (ctrl is DropDownList)
                {
                    if (((DropDownList)ctrl).SelectedIndex == 0)
                    {
                        Response.Write("<script>alert('Por favor, selecione todos os professores.');</script>"); ;
                        return false;
                    }
                }
            }
            return true;
        }

        private void GerarDDLs()
        {
            int turmaId = Convert.ToInt32(ddlCursoNaoAtribuido.SelectedValue);
            var turma = container.TurmaSet.Where(t => t.Id == turmaId).First();

            var curso = container.CursoSet.Where(c => c.Id == turma.Curso_Id).FirstOrDefault();

            pnlItensDinamicos.Controls.Add(new LiteralControl("<h4>Matérias</h4>"));

            foreach (MateriaSet materia in curso.MateriaSet)
            {
                DropDownList ddlMateria = new DropDownList();

                ddlMateria.ID = "ddl" + materia.nmMateria.Trim() + "_" + materia.Id;

                pnlItensDinamicos.Controls.Add(new LiteralControl("<br /><div class='control-group'><label class='control-label' for='txtNomeCompleto'> " + materia.nmMateria + "</label><div class='controls'>"));
                pnlItensDinamicos.Controls.Add(ddlMateria);

                ddlMateria.DataSource = materia.ProfessorSet;
                ddlMateria.DataTextField = "nmProfessor";
                ddlMateria.DataValueField = "Id";
                ddlMateria.DataBind();
                ddlMateria.Items.Insert(0, "Selecione o professor...");

                pnlItensDinamicos.Controls.Add(new LiteralControl("</div></div>"));
            }
        }

        #endregion
    }
}